<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/26
 * Time: 16:06
 */

class Async extends M_Controller
{
    //发送短信验证码
    public function sms_send(){
        $phone = $this->input->get_post('phone');
        $is_type = $this->input->get_post('is_type');
//        $phone = "18201994564";
//        $is_type = 1;
        if(!$phone){
            exit($this->callback_json(
                array(
                    'msg'=>'手机号码为空',
                    'code'=>0
                )

            ));
        }
        if(!is_mobile($phone)){
            exit($this->callback_json(
                array(
                    'msg'=>'手机号码格式错误',
                    'code'=>0
                )

            ));
        }
        //查询用户信息
        $users = $this->db->where('mobile',$phone)->get('user')->row_array();
//        print_r($phone);
//        print_r($users);
//        exit;
        //根据手机号码查询是否有效
        $sms = $this->db->where('phone',$phone)->get('sms_code')->row_array();
//        $s_id = isset($sms['id'])?$sms['id']:0;
        if($is_type == 1){
            if($users){
                exit($this->callback_json(
                    array(
                        'msg'=>'手机号码已经注册',
                        'code'=>0
                    )

                ));
            }
        }else{
//            if(!$users){
//                exit($this->callback_json(
//                    array(
//                        'msg'=>'手机号码不存在',
//                        'code'=>0
//                    )
//                ));
//            }
        }
        $code = '';
        //判断该手机号码发送次数
        $today = strtotime('today');
        $where['phone'] = $phone;
        $where['type'] = $is_type;
        $where['addtime>='] = $today;
        $rt = $this->db->where($where)->get('sms_code')->row_array();
        if($rt && $rt['num']>=10){
            exit($this->callback_json(
                array(
                    'msg'=>'短信验证码发送超出限制',
                    'code'=>0
                )
            ));
        }
        $click_num = 1;
        if($rt && $rt['num']){
            $click_num = $rt['num']+1;
        }
        //判断短信验证码是否有用 如果没有用  直接返回(暂时不发送)
       $wh['is_last'] = 1;
       $wh['type'] = $is_type;
       $res = $this->db->where($wh)->get('sms_code')->row_array();
       $sms_id = 0;
       $code = '';
       //已经有发送过验证码
       if($res && $res['id']){
            //生成短信日志
           $smw = array();
           $smw['id'] = $res['id'];
           $data = array();
           $data['num'] = $res['num']+1;
           //更新发送次数
           $this->db->where($smw)->update('sms_code', $data);
           $left_code = time()- $res['addtime'];
           //小于30分钟不变
           if($left_code < 60*30){
               $code = $res['code'];
           }else{
               $code = rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9);
               $this->sms_new_code($code, $phone,$is_type);

           }

       }else{
           $code = rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9);
           $this->sms_new_code($code, $phone,$is_type);
       }
       sendSms($phone,$code,1);

        exit($this->callback_json(array(
            'msg' => '发送验证码成功',
            'code' => 1
        )));

    }
    //发送新验证码
     function sms_new_code($code, $phone,$is_type=1){

         $sms_data = array();
         $sms_data['phone'] = $phone;
         $sms_data['code'] = $code;
         $sms_data['addtime'] = time();
         $sms_data['ip'] = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"]; //ip2long(isset($data['ip']) ? $data['ip'] : '');
         $sms_data['is_last'] = 1;
         $sms_data['type'] = $is_type;

         $sms_id = $this->db->insert('sms_code',$sms_data);
         if($sms_id>1){
             //更新早的验证码
             $where = array();
             $where['phone'] = $phone;
             $where['id <'] = $sms_id;
             $where['type'] = $is_type;
             $data = array();
             $data['is_last'] = 0;
             $this->ci->db->where($where)->update('sms_code',$data);
             $this->ci->db->trans_complete();
         }

     }

}